<?php

require_once 'Base.php';


class OrderModel extends Base {
	
	//private $_table = "shop_goods";
	
	public function getList($where='', $page='',$pagesize=''){
		
		if($where) $this->where($where);
		//echo 
		
		if($pagesize) $this->limitPage($page,$pagesize);
		$this->orderby("oid desc");
	    $orderlist=$this->select();
	
	    $this->_db = $this->getAdapter();
	  
	   
	   foreach ($orderlist as &$val){
	   	 $sellerid=$val['sellerid'];
	   	  $sellerone=$this->_db->fetchRow("select * from shop_seller where id=$sellerid");
	   	  $val['sellername']=$sellerone['name'];
	   	  $val['send_price']=$sellerone['send_price'];
	   	  $val['total_price']=$sellerone['send_price']+$val['price'];
	   	  $val['seller_thumb']=$sellerone['thumb'];
	   	  $val['ctime']=date('Y-m-d H:i');
	   	  if($val['status']==0){$val['status']='未确认';}
	   	  if($val['status']==1){$val['status']='未付款';}
	   	  if($val['status']==2){$val['status']='已付款';}
	   	  if($val['status']==3){$val['status']='已发货';}
	   	  if($val['status']==4){$val['status']='已完成';}
	   	  $orderid=$val['oid'];
	   	
	   	  $ordergoods=$this->_db->fetchAll("select shop_order_goods.*,shop_goods.name,shop_goods.thumb goods_thumb
	   	  		 from shop_order_goods left join shop_goods
	   	  		on shop_order_goods.goodsid=shop_goods.id
	   	  		 where shop_order_goods.orderid=$orderid ");
	   	
	   	  $val['goodsname']=$ordergoods[0]['name'].'等'.count($ordergoods).'件商品';
	   	  $val['goods']=$ordergoods;
	   }
		//print_r($temp);
		return $orderlist;
	}
	
	public function getone($where){
	   
	  $this->_db = $this->getAdapter();
    	$select = $this->_db->select();
    	$select->from($this->_table);
    	foreach ($where as $key => $val) {
            if (is_int($key)) {
               $select->where($val);
            } else {
               $select->where($key, $val);
            }
	     }   
	     
	   return $this->_db->fetchRow($select);
// 		 $this->where($where);
// 		 return $this->fetch();
	}
	
	public function updateorder($data,$where){
		$this->_db = $this->getAdapter();
		
		
		return $this->_db->update("shop_order", $data,$where);
	}
	
	public function addorder($data){
		return $this->add($data);
	}
	
	public function addorder_goods($data){
		$this->_db = $this->getAdapter();
	  $this->_db->insert("shop_order_goods", $data);
	}
	
	//public function addorder_goods($data){
	//	$this->_db = $this->getAdapter();
	//	$this->_db->insert("shop_order_goods", $data);
	//}
	
	public function addweixin_paylog($data){
		$this->_db = $this->getAdapter();
		$this->_db->insert("shop_weixin_paylogs", $data);
	}
	
	public function order_goods_list($where){
		$this->_db = $this->getAdapter();
		$select = $this->_db->select();
		$select->from("shop_order_goods");
	//	print_r($where);
		foreach ($where as $key => $val) {
			if (is_int($key)) {
				$select->where($val);
			} else {
				$select->where($key, $val);
			}
		}
		return $this->_db->fetchAll($select);
		//return $this->_db->insert("shop_order_goods", $data);
	}
	
	
	
	
}
